﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyAction : SSAction
{
    float gravity;          // 重力加速度
    float speed;            // 初始速度
    float time;             // 时间
    Vector3 direction;      // 初始飞行方向

    public static FlyAction GetSSAction(Vector3 direction, float speed)
    {
        FlyAction action = ScriptableObject.CreateInstance<FlyAction>();
        action.gravity = 9.8f;
        action.time = 0;
        action.speed = speed;
        action.direction = direction;
        return action;
    }

    public override void Start()
    {
        // 初始飞行模式为运动学模式
        gameObject.GetComponent<Rigidbody>().isKinematic = true;
    }

    public override void Update()
    {
        time += Time.deltaTime;
        // 竖直方向上有重力加速度
        transform.Translate(Vector3.down * gravity * time * Time.deltaTime);
        // 初始飞行方向匀速运动
        transform.Translate(direction * speed * Time.deltaTime);
        // 飞碟到达画面底部，动作结束，进行回调
        if (this.transform.position.y < -6) 
        {
            this.destroy = true;
            this.enable = false;
            this.callback.SSActionEvent(this);
        }
        
    }
}
